package com.hszl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hszl.blog.dao.mapper.TagMapper;
import com.hszl.blog.dao.pojo.Tag;
import com.hszl.blog.service.TagService;
import com.hszl.blog.vo.Result;
import com.hszl.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author: 胡爽
 * @date: 2022/5/20 11:30
 **/
@Service
public class TagServiceImpl implements TagService {
    /**
     * 这里只能使用xml文件查询tag标签内
     */
    @Autowired
    private TagMapper tagMapper;
    public TagVo copy(Tag tag){
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        return tagVo;
    }
    public List<TagVo> copyList(List<Tag> tagList){
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    /**
     * 根据文章id查询所有标签
     * @param articleId
     * @return
     */
    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        List<Tag> tagList = tagMapper.findTagsByArticleId(articleId);
        return copyList(tagList);
    }

    @Override
    public List<TagVo> hot(int limit) {
        List<Long> hotsTagIds = tagMapper.findHotsTagIds(limit);
        if (CollectionUtils.isEmpty(hotsTagIds)){

            return Collections.emptyList();
        }

        List<Tag> tagList = tagMapper.findTagsByTagIds(hotsTagIds);
        return copyList(tagList);
    }

    @Override
    public Result findAllTags() {
        List<Tag> tagList = tagMapper.selectList(new QueryWrapper<>());
        return Result.success(copyList(tagList));
    }

    @Override
    public Result findAllDetailTags() {
        List<Tag> tagList = tagMapper.selectList(new QueryWrapper<>());
        return Result.success(copyList(tagList));
    }

    @Override
    public Result findAllDetailTagsById(Long id) {
        Tag tag = tagMapper.selectById(id);

        return Result.success(copy(tag));
    }
}
